


import pymysql


host = '172.16.36.120'
port = 3306
user = 'root'
password = '123456'
database = 'gxzdc'
charset = 'utf8'

def get_conn():
    db = pymysql.Connect(host=host,port=port,user=user,password=password,database=database,charset=charset)
     # print('数据库已链接')
    return db
def read_data(sql):
    db =get_conn()
    cursor = db.cursor()
    data = None
    try:
        cursor.execute(sql)
        data = cursor.fetchall()
#             print('记录已读取')
    except Exception as e:
        print(e)
    cursor.close()
    db.close()
    return data

def write_data(sql):
    db = get_conn()
    cursor = db.cursor()
    rows = 0
    try:
         rows = cursor.execute(sql)
         db.commit()
        # print('录取已写入')
    except Exception as e:
        db.rollback()
        print(e)

    cursor.close()
    db.close()
    return rows
if _name_ == '_main_':


    sql = "update users set password = '123'"
print(write_data(sql))















